package role

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRoleLogic {
	return &UpdateRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateRoleLogic) UpdateRole(req *types.UpdateRoleReq) (resp *types.UpdateRoleResp, err error) {
	// 1.builder rpc request
	updateRoleReq := pb.UpdateRoleReq{RoleId: req.RoleId, RoleName: req.RoleName, RoleRemark: req.RoleRemark,
		DeptIdArr: req.DeptIdArr, DeptId: req.DeptId, MenuIdArr: req.MenuIdList}
	// 2.request rpc
	updateRole, err := l.svcCtx.UserCenterClient.UpdateRole(l.ctx, &updateRoleReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	return &types.UpdateRoleResp{
		RoleId: updateRole.RoleId,
	}, nil
}
